package com.quanyan.stadium.web.api.v2;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.stadium.api.req.ReqId;
import com.quanyan.stadium.api.req.ReqRecordGameScheduleResult;
import com.quanyan.stadium.api.req.gameEventConfig.ReqPersonCreateGameEvent;
import com.quanyan.stadium.api.resp.RespPersonalGameEventDetail;
import com.quanyan.stadium.api.resp.RespScoreMsg;
import com.quanyan.stadium.entity.vo.GameEvent;
import com.quanyan.stadium.enums.GameSystemConfigEnum;
import com.quanyan.stadium.service.GameEventService;
import com.quanyan.stadium.service.gameEvent.PersonGameEventConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

@RestController("apiPersonalGameEvent")
@RequestMapping("/api/stadium/personalGameEvent")
public class PersonalGameEventController {

    private final static Logger logger = LoggerFactory.getLogger(PersonalGameEventController.class);

    @Autowired
    private PersonGameEventConfigService personGameEventConfigService;

    @Autowired
    private GameEventService gameEventService;

    /**
     * 个人赛----创建赛事
     * */
    @RequestMapping(value="/createPersonalGame", method = RequestMethod.POST)
    public APIResponse<?> createPersonalGame(@RequestBody @Valid ReqPersonCreateGameEvent personalGameEvent, BindingResult result, HttpServletRequest request) throws Exception {

        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        Integer uid = (Integer)request.getAttribute("uid");

        if (personalGameEvent.getGameSystemType().intValue() == GameSystemConfigEnum.CONFIG_GAME_TYPE_PK.getId() && personalGameEvent.getTeamLimitNum().intValue() != 2) {
            logger.info("createPersonalGame teamLimitNum not 2.");
            return APIResponse.returnFail("参赛人数超出边界值");
        }
        if (personalGameEvent.getGameSystemType().intValue() == GameSystemConfigEnum.CONFIG_GAME_TYPE_MIXED.getId() && personalGameEvent.getTeamLimitNum().intValue() > 128) {
            return APIResponse.returnFail("混合赛参赛人数不能超过128");
        }
        if (personalGameEvent.getStartTime() <= new Date().getTime()) {
            return APIResponse.returnFail("赛事开始时间不能小于当前时间");
        }

        logger.info("createPersonalGame personalGameEvent:"+personalGameEvent.toString()+" uid:"+uid);
        GameEvent gameEvent = personGameEventConfigService.createGameEvent(personalGameEvent, uid);
        return APIResponse.returnSuccess(gameEvent);
    }

    /**
     * 赛事编辑-----获取某栏目下的可用赛制类别和玩法类别
     *
     * */
    @RequestMapping(value="/gameCategoryAndSystem", method = RequestMethod.POST)
    public APIResponse<?> gameCategoryAndSystem(@RequestBody ReqId reqId, BindingResult result)  {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getCategoryId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getGameType()) {
            reqId.setGameType((byte)0);
        }
        return personGameEventConfigService.gameCategoryAndSystem(reqId.getCategoryId(), reqId.getGameType());
    }

    /**
     * 个人对抗赛 —— 比分录入
     */
    @RequestMapping(value = "/recordResultForPersonalAgainst",method = RequestMethod.POST)
    public APIResponse<String> recordResultForPersonalAgainst(@RequestBody ReqRecordGameScheduleResult req, HttpServletRequest request){
        Integer uid = (Integer)request.getAttribute("uid");
        return APIResponse.returnSuccess(gameEventService.recordResultForPersonalAgainst(uid,req));
    }

    /**
     * 个人对抗赛详情 —— 悬浮通知
     */
    @RequestMapping(value = "/getRecordResultMessage",method = RequestMethod.POST)
    public APIResponse<RespScoreMsg> getRecordResultMessage(@RequestBody ReqId reqId, HttpServletRequest request){
        Integer uid = (Integer)request.getAttribute("uid");
        if(null == reqId.getGameEventId()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return APIResponse.returnSuccess(gameEventService.getRecordResultMessage(reqId.getGameEventId(),uid));
    }

    /**
     * 个人对抗赛赛事详情
     */
    @RequestMapping(value = "/selectPersonalGameEventDetail",method = RequestMethod.POST)
    public APIResponse<RespPersonalGameEventDetail> selectPersonalGameEventDetail(@RequestBody ReqId reqId,BindingResult result,HttpServletRequest request){
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        Object id = request.getAttribute("uid");
        Integer uid = null;
        if(null == reqId.getGameEventId()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        if(null != id){
            uid = (Integer)id;
        }
        return APIResponse.returnSuccess(gameEventService.selectPersonalGameEventDetail(reqId.getGameEventId(), uid));
    }

}
